package com.zwj.multipledata.service;

import com.zwj.multipledata.mapper.crm.UserMapper;
import com.zwj.multipledata.mapper.trade.AccountMapper;
import com.zwj.multipledata.pojo.crm.User;
import com.zwj.multipledata.pojo.trade.Account;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BusinessService {
    @Resource
    UserMapper userMapper;
    @Resource
    AccountMapper accountMapper;

    public List<User> getUserAll() {
        return userMapper.getAll();
    }

    public List<Account> getAccountAll() {
        return accountMapper.getAll();
    }

    public String getUserBalance(String userId) {
        User user = userMapper.getUserById(userId);
        Account account = accountMapper.getAccountByUserId(userId);
        return user.getName() + "的余额为" + account.getAmount() + "。";
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateUserAndAccount(String userId, String accountId) {
        User user = new User();
        user.setId(userId);
        user.setAge(111);
        userMapper.updateById(user);
        int i = 1 / 0;
        Account account = new Account();
        account.setId(accountId);
        account.setAmount(1111.0);
        accountMapper.updateById(account);
    }
}